package com.ttw.demo;

import android.Manifest;
import android.app.Application;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.ttw.TTW;
import com.ttw.ttw.R;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.info);
        textView.setText("Android " + Build.VERSION.RELEASE);
        textView.append("\n" +
                Build.MANUFACTURER + " " +
                Build.BRAND + " " +
                Build.MODEL);

        textView.append("\nBoss直聘..");

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        }
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SYSTEM_ALERT_WINDOW) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);
        }
    }

    public void onClick(View view) {

        if (view.getId() == R.id.btn_kill) {
//            killApp();
            Intent intent = new Intent("com.ttw.demo.MY_BROCASTRECEIVER");
//第一个参数为广播接收器所在应用的包名
//第二个参数为广播接收器包名+类名
            intent.setComponent(new ComponentName("com.ttw.demo","com.ttw.demo.MyReceiver"));
            Bundle bundle=new Bundle();
            bundle.putString("key","Hello Receiver！");
            intent.putExtras(bundle);
            sendBroadcast(intent);
            return;
        }
        if (view.getId() == R.id.hideicon) {
            return;
        }
        if (view.getId() == R.id.clean) {

        }
        if (view.getId() == R.id.showicon) {
            return;
        }
        if (view.getId() == R.id.bizhi) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "这个功能需要文件读权限", Toast.LENGTH_SHORT).show();
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
                return;
            }
            TTW.setLiveWallpaper(this);
            return;
        }
        if (view.getId() == R.id.inputbbb) {
//            ActivityUtils2.startBySyncService(this, new Intent(this, ScreenLockActivity.class), "screenlock");
            return;
        }
        if (view.getId() == R.id.baimingdan) {
            TTW.whiteListActivity(this, "TTW");
            return;
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        TTW.setLiveWallpaper(this);
    }

    private void killApp() {
        Intent mIntent = new Intent();
        mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            mIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            mIntent.setAction(Intent.ACTION_VIEW);
            mIntent.setClassName(
                    "com.android.settings",
                    "com.android.setting.InstalledAppDetails"
            );
            mIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        startActivity(mIntent);
    }

    public static void start(Application application){
        application.startActivity(new Intent(application,MainActivity.class));
    }
}